#include "music.h"
#include<QMediaPlayer>
#include<QCoreApplication>
#include<QUuid>
Music::Music()
    :isLike(false)
    ,isHistory(false)
{

}

Music::Music(QUrl url)
    :isLike(false)
    ,isHistory(false)
    ,musicUrl(url)
{
    musicUuid=QUuid::createUuid().toString();
    praseMediaMetaMusic();

}

void Music::setMusicName(const QString &musicName)
{
    this->musicName=musicName;
}

void Music::setMusicSinger(const QString &musicSinger)
{
    this->musicSinger=musicSinger;
}

void Music::setMusicAlbum(const QString &musicAlbum)
{
    this->musicAlbum=musicAlbum;
}

void Music::setMusicDuration(qint64 duration)
{
    this->duration=duration;
}

void Music::setIsLike(bool isLike)
{
    this->isLike=isLike;
}

void Music::setIsHistory(bool isHistory)
{
    this->isHistory=isHistory;
}

void Music::setMusicPath(QUrl musicUrl)
{
    this->musicUrl=musicUrl;
}

QString Music::getMusicName() const
{
    return musicName;
}

QString Music::getMusicSinger() const
{
    return musicSinger;
}

QString Music::getMusicAlbum() const
{
    return musicAlbum;
}

qint64 Music::getMusicDuration() const
{
    return duration;
}

bool Music::getIsLike() const
{
    return isLike;
}

bool Music::getIsHistory() const
{
    return isHistory;
}

QUrl Music::getMusicPath() const
{
    return musicUrl;
}

QString Music::getMusicUuid() const
{
    return musicUuid;
}

QString Music::getLrcFilePath()
{
    QString musicLrcPath=musicUrl.toLocalFile();
    musicLrcPath.replace(".mp3",".lrc");
    musicLrcPath.replace(".flac",".lrc");
    musicLrcPath.replace(".mpga",".lrc");

    return musicLrcPath;
}

void Music::praseMediaMetaMusic()
{
    QMediaPlayer player;
    player.setMedia(musicUrl);//setMedia不会等待歌曲加载完再返回，而是直接返回，所以需要一个循环等待
    while (!player.isMetaDataAvailable()) {
        QCoreApplication::processEvents();
    }
    if(player.isMetaDataAvailable())
    {
        musicName=player.metaData("Title").toString();
        musicSinger=player.metaData("Author").toString();
        musicAlbum=player.metaData("AlbumTitle").toString();
        duration=player.metaData("Duration").toLongLong();

        QString fileName=musicUrl.fileName();
        //2002年的第一场雪 - 刀郎.mp3
        int index=fileName.indexOf('-');
        if(musicName.isEmpty())
        {
            if(index!=-1)
            {
              //2002年的第一场雪 - 刀郎.mp3
              musicName=fileName.mid(0,index).trimmed();
            }
            else{
              //2002年的第一场雪.mp3
                musicName=fileName.mid(0,fileName.indexOf('.')).trimmed();
            }
        }
        if(musicSinger.isEmpty())
        {
            if(index!=-1)
            {
                //2002年的第一场雪 - 刀郎.mp3
                musicSinger=fileName.mid(index+1,fileName.indexOf('.')-index-1).trimmed();
            }
            else
            {
                //2002年的第一场雪.mp3
                musicSinger="未知歌手";
            }
        }
        if(musicAlbum.isEmpty())
        {
            musicAlbum="未知专辑";
        }
        qDebug()<<musicName<<" "<<musicSinger<<" "<<musicAlbum<<" "<<duration;
    }

}
